/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GestionVIP.java
 *
 * Created on 1 juin 2011, 14:36:40
 */

package cannes.gui;

import cannes.bean.vip.Vip;
import cannes.dao.DAO;
import cannes.dao.DAOFactory;
import java.util.ArrayList;
import javax.swing.JFrame;

/**
 *
 * @author p1008977
 */
public class GestionVIP extends javax.swing.JPanel {

    /** Creates new form GestionVIP */
    public GestionVIP() {
        initComponents();
        initListe();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    private void initListe(){
        listeVIP.setModel(listModel);
        vipDAO = DAOFactory.getVipDAO();
        liste = vipDAO.findAll();
        for (Vip vip : liste) {
            listModel.addElement(vip.getNom() + " " + vip.getPrenom());
        }
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listeVIP = new javax.swing.JList();
        nouveau = new javax.swing.JButton();
        modifier = new javax.swing.JButton();
        supprimer = new javax.swing.JButton();
        infoVIP = new javax.swing.JButton();

        listeVIP.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listeVIPValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listeVIP);

        nouveau.setText("Nouveau");
        nouveau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nouveauActionPerformed(evt);
            }
        });

        modifier.setText("Modifier");
        modifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifierActionPerformed(evt);
            }
        });

        supprimer.setText("Supprimer");
        supprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supprimerActionPerformed(evt);
            }
        });

        infoVIP.setText("Infos VIP");
        infoVIP.setEnabled(false);
        infoVIP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                infoVIPActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(infoVIP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(supprimer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(modifier, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nouveau, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(infoVIP)
                        .addGap(18, 18, 18)
                        .addComponent(nouveau)
                        .addGap(18, 18, 18)
                        .addComponent(modifier)
                        .addGap(18, 18, 18)
                        .addComponent(supprimer)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void listeVIPValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listeVIPValueChanged
        infoVIP.setEnabled(true);
    }//GEN-LAST:event_listeVIPValueChanged

    private void infoVIPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_infoVIPActionPerformed
        
        Vip vip = liste.get(listeVIP.getSelectedIndex());
        InfosVIP iv = new InfosVIP(vip);
        iv.setVisible(true);
        JFrame info = new JFrame("Information VIP");
        info.add(iv);
        info.setSize(900,500);
        info.setVisible(true);
    }//GEN-LAST:event_infoVIPActionPerformed

    private void nouveauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nouveauActionPerformed
        InfosVIP iv = new InfosVIP(new Vip());
        iv.setVisible(true);
        JFrame info = new JFrame("Information VIP");
        info.add(iv);
        info.setSize(900,500);
        info.setVisible(true);
        iv.nouveau();

    }//GEN-LAST:event_nouveauActionPerformed

    private void modifierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifierActionPerformed
        Vip vip = liste.get(listeVIP.getSelectedIndex());
        InfosVIP iv = new InfosVIP(vip);
        iv.setVisible(true);
        JFrame info = new JFrame("Information VIP");
        info.add(iv);
        info.setSize(900,500);
        info.setVisible(true);
        iv.modif();
    }//GEN-LAST:event_modifierActionPerformed

    private void supprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supprimerActionPerformed
        vipDAO = DAOFactory.getVipDAO();
        Vip vip = liste.get(listeVIP.getSelectedIndex());
        vipDAO.delete(vip);
        listModel.remove(listeVIP.getSelectedIndex());
    }//GEN-LAST:event_supprimerActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton infoVIP;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listeVIP;
    private javax.swing.JButton modifier;
    private javax.swing.JButton nouveau;
    private javax.swing.JButton supprimer;
    // End of variables declaration//GEN-END:variables
    javax.swing.DefaultListModel listModel = new javax.swing.DefaultListModel();
    DAO<Vip> vipDAO;
    ArrayList<Vip> liste;
}
